package com.tistory.admin.common;

import java.util.Locale;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**<pre>
 *description 	: 발생한 에러에 대한 Base Runtime Exception
 *update history
 *-------------------------------------------------
 *</pre> 
 */
@SuppressWarnings("unused")
public class BaseException extends RuntimeException {

	private static final long serialVersionUID = 1145086290326298002L;
	private static Logger logger = Logger.getLogger(BaseException.class);
	private String domain;
	private String code;
	private String messageCode;
	private Object[] args;
	
	public String getMessage(String code) {
		String message="메세지 코드["+code+"]가 messages.properties에 정의가 되지 않았습니다. ";
		try {
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return message;
	}
	


	public String getMessage(String code, Object[] args) {
		String message="메세지 코드["+code+"]가 messages.properties에 정의가 되지 않았습니다. ";
		try {
            /** @TODO		    
		    // 메세지처리가들어가야함// message = WCommon.getMessage(code,args,"",Locale.getDefault());
		     * 
		     */
		} catch(Exception e) {
			e.printStackTrace();
		}
		return message;
	}
	
	
	public BaseException(String domain, String code) {
		super("errors." + domain + "." + code);
		this.domain = domain;
		this.code = code;
		this.messageCode = "errors." + domain + "." + code;
		logger.error(getMessage(messageCode));
	}
	
	public BaseException(String domain, String code,Throwable causeThrowable) {
		super("errors." + domain + "." + code,causeThrowable);
		this.domain = domain;
		this.code = code;
		this.messageCode = "errors." + domain + "." + code;
	}
	
	public BaseException(String domain, String code, Object[] args, Throwable causeThrowable) {
		super("errors." + domain + "." + code,causeThrowable);
		this.domain = domain;
		this.code = code;
		this.args = args;
		this.messageCode = "errors." + domain + "." + code;
	}
	
	public BaseException(String domain, String code, Object[] args) {
		super("errors." + domain + "." + code);
		this.domain = domain;
		this.code = code;
		this.args = args;
		this.messageCode = "errors." + domain + "." + code;
		logger.error(getMessage(messageCode, args));
	}
		
	
	public BaseException(String messageKey) {
		
		super(messageKey);
		messageCode=messageKey;
		logger.error(getMessage(messageCode));

	}	
	
	public BaseException(String messageKey,Throwable causeThrowable) {
		super(messageKey ,causeThrowable);
		messageCode=messageKey;
	}	

	public String getAdminCode() {
		return messageCode + ".admin";
	}

	public String getUserCode() {
		return messageCode + ".user";
	}

	public String getMessageCode() {
		return messageCode;
	}

	public void setMessageCode(String messageCode) {
		this.messageCode = messageCode;
	}
	
	public String getAdminMessage() {
		if(args != null) {
			return getMessage(getAdminCode(), args);
		}else return getMessage(getAdminCode());
	}

	public String getUserMessage() {
		if(args != null)
		{
			return getMessage(getUserCode(), args);
		}else
			return getMessage(getUserCode());
	}
	
	public String getDefaultMessage() {
		if(args != null){
			return getMessage(messageCode, args);
		}
		else
			return getMessage(messageCode);
	}


	public Object[] getArgs() {
		return args;
	}

	public void setArgs(Object[] args) {
		this.args = args;
	}
	

	@RequestMapping("/redirect")
	public String redirect(@RequestParam("url") String url) {
		return "redirect:" + url;
	}
		
}
